<!DOCTYPE html>
<html lang="en" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro"
      xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <link rel="stylesheet" href="/layui/css/layui.css">
  <link rel="stylesheet" href="/css/custom.form.css">
</head>
<body>
<div class="panel panel-default operation" hidden>
  <div class="panel-heading title"></div>
</div>

<input hidden id="stationId">
<input hidden id="time">
<input hidden id="taxorgcode">

<div class="table_div1">
  <div id="searchParam"  >
    <div class="layui-form-item">
      <div class="layui-input-inline layui-form">
      	<select id="taxorgcodeInput">
		</select>
      </div>
      
      <!-- <div class="layui-input-inline">
        <input type="text" id="stationNameInput" class="layui-input"  autocomplete="off" placeholder="请输入企业名称">
      </div> -->
      
	  <div class="layui-input-inline">
        <input type="text" id="yearMonthInput" class="layui-input"  autocomplete="off" placeholder="请输入年月">
      </div>
      <div class="layui-input-inline layui-form">
        <select id="industryInput">
        	<option value="">请选择行业</option>
        	<option value="铁矿采矿">铁矿采矿</option>
        	<option value="铁矿选矿">铁矿选矿</option>
        	<option value="水泥制造">水泥制造</option>
        	<option value="混凝土">混凝土</option>
        	
		</select>
      </div>
      <div class="layui-input-inline ">
        <button class="layui-btn" onclick="search1()"  id="search">查询</button>
        <button class="layui-btn" id="export1">导出</button>
      </div>
    </div>
  </div>
  <table class="layui-table" id="showTable1" lay-filter="showTable1" ></table>
</div>

<div class="table_div2" hidden>
  <div id="searchParam"  >
    <div class="layui-form-item">
      <div class="layui-input-inline">
        <input type="text" id="stationinfoNameInput" class="layui-input" autocomplete="off" placeholder="请输入企业名称">
      </div>
      
      <div class="layui-input-inline">
        <button class="layui-btn" onclick="search2()" id="search2">查询</button>
      </div>
      
      <div class="layui-input-inline ">
        <button class="layui-btn" id="back2">返回</button>
        <button class="layui-btn" id="export2">导出</button>
      </div>
    </div>

  </div>
  <table class="layui-table" id="showTable2" lay-filter="showTable2" ></table>
</div>


<script type="text/html" id="toolbar">
  <div class="layui-btn-container">
  </div>
</script>



<script type="text/html" id="tool1">
  <a class="layui-btn layui-btn-xs" lay-event="view" >查看企业</a>
</script>

</body>
</html>
<script src="/layui/layui.all.js"></script>
<script src="/js/core.util.js"></script>
<script>
  //获取token
  var token = CoreUtil.getData("access_token");
  //地址栏转义token中的#号
  var tokenQuery = token.replace("#", "%23");
  
  var tableIns1;
  var tableIns2;
  
  var table = layui.table;
  var form = layui.form;
  var layer = layui.layer;
  var $ = jQuery = layui.jquery;
  var laydate = layui.laydate;
  
  //初始当前 年月
  var currentDate = new Date();
  var currentYear = currentDate.getFullYear();
  var currentMonth = currentDate.getMonth() + 1; // 月份从0开始，所以要加1
  var yearMonthInput;
  if(10 > currentMonth > 0){
	  yearMonthInput = currentYear + "-0" + currentMonth;
	  $("#yearMonthInput").val(currentYear + "-0" + currentMonth);
  }else{
	  yearMonthInput = currentYear + "" + currentMonth;
	  $("#yearMonthInput").val(currentYear + "-" + currentMonth);
  }
  
  
  $(function() {
	  // 加载机关
      CoreUtil.sendGet("/sys/deptlist",null , function(res) {
			if (res.code == '0') {
				var value = res.data[0];
				var opt = "<option value = ''>请选择所属机关</option>";
				for (var i in value) {
					opt += '<option value = "' + value[i].id + '">'
							+ value[i].name + '</option>';
				}
				
				$("#taxorgcodeInput").html(opt);
				form.render('select'); //需要渲染一下
			} else {
				layer.msg(res.msg);
				return false;
			}
		}, false, false);
		
  });
  
  
  layui.use(['table', 'layer', 'laydate'], function () {
	  laydate.render({
		    elem: '#yearMonthInput',
		    type: 'month', // 设置类型为年选择
		    format: 'yyyy-MM', // 设置年月格式
		    trigger: 'click', // 设置触发方式
		  });
	  
    //加载table
    tableIns1 = table.render({
      elem: '#showTable1'
      , contentType: 'application/json'
      , headers: {"authorization": token}
      , page: true //开启分页
      , url: '/nr/stationforecastmonth/list0' //数据接口
      , where: {time: yearMonthInput} // 初始化时的过滤条件
			
      , method: 'POST'
      , parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
        return {
          "code": res.code, //解析接口状态
          "msg": res.msg, //解析提示文本
          "count": CoreUtil.isEmpty(res.data) ? 0 : res.data.total, //解析数据长度
          "data": CoreUtil.isEmpty(res.data) ? null : res.data.records //解析数据列表
        }
      }
      , cols: [
        [
          {type: 'checkbox', fixed: 'left'},
          {field: 'id', title: '税务机关代码', sort: true, hide: true},
          {field: 'yearMonths', title: '年月', sort: true}, 
          {field: 'name', title: '所属机关', sort: true}, 
          
          {field: 'gatherValue', title: '采集笔数', sort: true},
          
          {field: 'forecastValue', title: '预估笔数', sort: true},
          
          {width: 120, toolbar: "#tool1", title: '操作', fixed: 'right'}
        ]
      ]
      , toolbar: '#toolbar1'
    });
	
    
    //加载table
    tableIns2 = table.render({
      elem: '#showTable2'
      , contentType: 'application/json'
      , headers: {"authorization": token}
      , page: true //开启分页
      , url: '/nr/stationforecastmonth/list' //数据接口
      , where: {time: yearMonthInput} // 初始化时的过滤条件
			
      , method: 'POST'
      , parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
        return {
          "code": res.code, //解析接口状态
          "msg": res.msg, //解析提示文本
          "count": CoreUtil.isEmpty(res.data) ? 0 : res.data.total, //解析数据长度
          "data": CoreUtil.isEmpty(res.data) ? null : res.data.records //解析数据列表
        }
      }
      , cols: [
        [
          {type: 'checkbox', fixed: 'left'},
          {field: 'id', title: '主键', sort: true, hide: true},
          
          {field: 'time', title: '年月', sort: true}, 
          {field: 'taxorgName', title: '所属机关', sort: true}, 
          {field: 'taxpayerName', title: '纳税人名称', sort: true},
          {field: 'taxpayerCode', title: '纳税人识别号', sort: true,
        	  templet:function(d){return d.taxpayerCode + "\t"}
          },
          {field: 'stationinfoName', title: '企业名称', sort: true},
          {field: 'forecastValue', title: '预估笔数', sort: true,
        	  templet:function(d){return d.forecastValue + "\t"}},
          {field: 'gatherValue', title: '采集笔数', sort: true,
        	  templet:function(d){return d.forecastValue + "\t"}},
          {field: 'gatherDay', title: '采集天数', sort: true},
          {field: 'gatherDay1', title: '采集天数(底图)', sort: true},
          {field: 'offlineTime', title: '离线时间',sort: true, hide: true},
        ]
      ]
      , toolbar: '#toolbar2'
    });
    
    //列操作
    table.on('tool(showTable1)',function (obj) {
      var data = obj.data;
      switch (obj.event) {
        case 'view':
        	
        	$("#taxorgcode").val(data.id);
        	search2();
        	$("#nameInput").val("");
        	$(".table_div1").hide();
        	$(".table_div2").show();
          break;
      }
    });
    
    
    $('#back2').on('click', function () {
    	$(".table_div1").show();
    	$(".table_div2").hide();
        return false;
    });
    
    
    //导出
    $('#export1').on('click', function () {
    		var time = $("#yearMonthInput").val();
    		//原先分页limit
		      var exportParams = {
		          limit: -1,
		    	  taxorgcode : $("#taxorgcodeInput").val(),
		    	  time : time,
		    	  industry : $("#industryInput").val()
		      };
      CoreUtil.sendPost("/nr/stationforecastmonth/list0", exportParams, function (res) {
        //初始化渲染数据
        if (res.data != null && res.data.records != null) {
          table.exportFile(tableIns1.config.id, res.data.records, 'xls');
        }
      });
    });
    
    
    //导出
    $('#export2').on('click', function () {
    	var time = $("#yearMonthInput").val();
    		//原先分页limit
	    var exportParams = {
	          limit: -1,
	    	  taxorgcode : $("#taxorgcode").val(),
	    	  time : time,
	    	  industry : $("#industryInput").val()
	    };
      	CoreUtil.sendPost("/nr/stationforecastmonth/list", exportParams, function (res) {
	        //初始化渲染数据
	        if (res.data != null && res.data.records != null) {
	          table.exportFile(tableIns2.config.id, res.data.records, 'xls');
	        }
      	});
     });
    
    
  });
	
  
  
  
  //执行查询
  function search1() {
	var time = $("#yearMonthInput").val();
	//这里以搜索为例
    tableIns1.reload({
      where: { //设定异步数据接口的额外参数，任意设
    	  taxorgcode : $("#taxorgcodeInput").val(),
    	  time : time,
    	  industry : $("#industryInput").val()
      }
      , page: {
        curr: 1 //重新从第 1 页开始
      }
    });
  };

  //执行查询
  function search2() {
	var time = $("#yearMonthInput").val();
	var stationinfoName = $("#stationinfoNameInput").val();
	//这里以搜索为例
    tableIns2.reload({
      where: { //设定异步数据接口的额外参数，任意设
    	  taxorgcode : $("#taxorgcode").val(),
    	  time : time,
    	  stationinfoName : stationinfoName,
    	  industry : $("#industryInput").val(),
      }
      , page: {
        curr: 1 //重新从第 1 页开始
      }
    });
  };
  
  
</script>